package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Ask;
import cn.wolfcode.luowowo.article.domain.Tag;
import cn.wolfcode.luowowo.article.mapper.AskMapper;
import cn.wolfcode.luowowo.article.query.AskQuery;
import cn.wolfcode.luowowo.article.service.IAskService;
import cn.wolfcode.luowowo.article.service.ITagService;
import cn.wolfcode.luowowo.cache.service.IQuestionRedisService;
import cn.wolfcode.luowowo.cache.vo.QuestionStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class AskServiceImpl implements IAskService {

    @Autowired
    private AskMapper askMapper;

    @Autowired
    private ITagService tagService;

    @Reference
    private IQuestionRedisService questionRedisService;

    @Override
    public PageInfo query(AskQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getTypeOrber());
        List<Ask> asks = askMapper.selectForQuery(qo);
        for (Ask ask : asks) {
            QuestionStatisVO vo = questionRedisService.getAskVO(ask.getId());
            System.out.println(vo.getBrowsenum()+"+11");
            ask.setVo(vo);
        }
        return new PageInfo(asks);
    }

    @Override
    public void upByStatus(Ask ask) {
        askMapper.updateByPrimaryKey(ask);
    }

    @Override
    public Ask getAsk(Long id) {

        return askMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Ask ask) {
        askMapper.insert(ask);
        String tags = ask.getTags();
        if (tags != null && tags !="" && tags.length()>0) {
            String[] split = tags.split(",");
            for (String tag : split) {
                Tag t = new Tag();
                t.setTags(tag);
                tagService.save(t);
                askMapper.saveByAidOrTid(ask.getId(),t.getId());
            }
        }
        questionRedisService.save(ask.getId());
    }

    @Override
    public List<Ask> listAll() {
        return askMapper.selectAll();
    }
}
